package com.symbio.dao.impl;

import com.symbio.dao.UserDao;
import com.symbio.domain.User;
import com.symbio.utils.JDBCUtils;
import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class UserDaoImpl implements UserDao {

    private JdbcTemplate template = new JdbcTemplate(JDBCUtils.getDataSource());

    @Override
    public List<User> findAll() {
        String sql = "select * from USER";
        List<User> users = template.query(sql, new BeanPropertyRowMapper<User>(User.class));
        return users;
    }

    @Override
    public User login(User user) {
        try {
            String sql = "select * from user where username = ? and password = ?";

            return template.queryForObject(sql,
                    new BeanPropertyRowMapper<User>(User.class),
                    user.getUsername(),
                    user.getPassword());
        } catch (DataAccessException exception) {
            exception.printStackTrace();
            return null;
        }
    }

    @Override
    public int add(User user) {
        String sql = "insert into user (name, gender, age, address, qq, email) values (?, ?, ?, ?, ?, ?)";
        return template.update(sql, user.getName(), user.getGender(), user.getAge(), user.getAddress(), user.getQq(), user.getEmail());
    }

    @Override
    public int delete(Integer id) {
        String sql = "delete from user where id = ?";
        return template.update(sql, id);
    }

    @Override
    public int update(User user) {
        String sql = "update user set name = ?, gender = ?, age = ?, address = ?, qq = ?, email = ? where id = ?";
        return template.update(sql, user.getName(), user.getGender(), user.getAge(), user.getAddress(), user.getQq(), user.getEmail(), user.getId());
    }

    @Override
    public User find(Integer id) {
        try {
            String sql = "select * from USER where id = ?";

            return template.queryForObject(sql,
                    new BeanPropertyRowMapper<User>(User.class),
                    id);
        } catch (DataAccessException exception) {
            exception.printStackTrace();
            return null;
        }
    }

    @Override
    public int findTotalCount(Map<String, String[]> parameterMap) {
        String sql = "select count(*) from user where 1 = 1";
        StringBuilder stringBuilder = new StringBuilder(sql);

        List<Object> list = new ArrayList<>();
        for (String key : parameterMap.keySet()) {
            String value = parameterMap.get(key)[0];
            if (!value.isEmpty() && !key.equals("currentPage") && !key.equals("rows")) {
                stringBuilder.append(" and " + key + " like ?");
                list.add("%" + value + "%");
            }
        }
        System.out.println(stringBuilder);
        System.out.println(list.toArray());
        return template.queryForObject(stringBuilder.toString(), Integer.class, list.toArray());
    }

    @Override
    public List<User> findByPage(int start, int rows, Map<String, String[]> parameterMap) {
        String sql = "select * from USER where 1 = 1";

        StringBuilder stringBuilder = new StringBuilder(sql);

        List<Object> list = new ArrayList<>();
        for (String key : parameterMap.keySet()) {
            String value = parameterMap.get(key)[0];
            if (!value.isEmpty() && !key.equals("currentPage") && !key.equals("rows")) {
                stringBuilder.append(" and " + key + " like ?");
                list.add("%" + value + "%");
            }
        }
        stringBuilder.append(" limit ?, ?");

        list.add(start);
        list.add(rows);

        System.out.println(stringBuilder);
        System.out.println(list.toArray());
        List<User> users = template.query(stringBuilder.toString(), new BeanPropertyRowMapper<User>(User.class), list.toArray());
        return users;
    }
}